package com.example.cpms.service.impl;

import com.example.cpms.entity.Plant;
import com.example.cpms.mapper.PlantMapper;
import com.example.cpms.service.PlantService;
import com.example.cpms.utils.PageBean;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 乔
 */
@Service
public class PlantServiceImpl implements PlantService {

    @Autowired
    private PlantMapper plantMapper;

    @Override
    public void delete(int id) {
        plantMapper.deleteById(id);
    }

     @Override
    public void add(Plant plant){
        plantMapper.insert(plant);

    }
    @Override
    public void update(Plant plant) {
        plantMapper.update(plant);
    }
    @Override
    public PageBean page(Integer page, Integer pageSize, String name) {
        PageHelper.startPage(page,pageSize);//使用PageHelper分页查询插件
        List<Plant> plantList = plantMapper.list(name); //从数据库获取列表数据
        Page<Plant> p = (Page<Plant>) plantList; //强转
        PageBean pageBean = new PageBean(p.getTotal(),p.getResult());
        return pageBean;
    }

    @Override
    public List<Plant> getAllPlant() {
        return plantMapper.findAll();
    }
}
